package com.analysis.service;

import com.analysis.entity.User;
import com.analysis.mapper.UserMapper;
import com.analysis.utils.PasswordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户服务类
 */
@Service
@Transactional
public class UserService {
    
    @Autowired
    private UserMapper userMapper;
    
    /**
     * 创建用户
     */
    public int createUser(User user) {
        if (user.getEnableStatus() == null) {
            user.setEnableStatus(1);
        }
        // 加密密码
        if (user.getPassword() != null && !PasswordUtils.isEncoded(user.getPassword())) {
            user.setPassword(PasswordUtils.encodePassword(user.getPassword()));
        }
        return userMapper.insert(user);
    }
    
    /**
     * 根据ID删除用户
     */
    public int deleteUser(Long id) {
        return userMapper.deleteById(id);
    }
    
    /**
     * 更新用户信息
     */
    public int updateUser(User user) {
        // 如果密码不为空且未加密，则进行加密
        if (user.getPassword() != null && !PasswordUtils.isEncoded(user.getPassword())) {
            user.setPassword(PasswordUtils.encodePassword(user.getPassword()));
        }
        return userMapper.update(user);
    }
    
    /**
     * 根据ID查询用户
     */
    public User getUserById(Long id) {
        return userMapper.selectById(id);
    }
    
    /**
     * 根据账号查询用户
     */
    public User getUserByNick(String nick) {
        return userMapper.selectByNick(nick);
    }
    
    /**
     * 根据手机号查询用户
     */
    public User getUserByMobile(String mobile) {
        return userMapper.selectByMobile(mobile);
    }
    
    /**
     * 根据微信openId查询用户
     */
    public User getUserByOpenId(String openId) {
        return userMapper.selectByOpenId(openId);
    }
    
    /**
     * 查询所有用户
     */
    public List<User> getAllUsers() {
        return userMapper.selectAll();
    }
    
    /**
     * 分页查询用户
     */
    public List<User> getUsersByPage(int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return userMapper.selectByPage(offset, pageSize);
    }
    
    /**
     * 统计用户总数
     */
    public int countUsers() {
        return userMapper.countAll();
    }
    
    /**
     * 根据条件查询用户
     */
    public List<User> getUsersByCondition(String nick, String mobile, Integer enableStatus) {
        return userMapper.selectByCondition(nick, mobile, enableStatus);
    }
    
    /**
     * 用户登录验证
     */
    public User login(String nick, String password) {
        User user = userMapper.selectByNick(nick);
        if (user != null && PasswordUtils.matches(password, user.getPassword())) {
            user.setPassword(null);
            return user;
        }
        return null;
    }
    
    /**
     * 检查账号是否存在
     */
    public boolean isNickExists(String nick) {
        return userMapper.selectByNick(nick) != null;
    }
    
    /**
     * 检查手机号是否存在
     */
    public boolean isMobileExists(String mobile) {
        return userMapper.selectByMobile(mobile) != null;
    }
    
    /**
     * 检查微信openId是否存在
     */
    public boolean isOpenIdExists(String openId) {
        return userMapper.selectByOpenId(openId) != null;
    }
}
